<?php

require_once(dirname(__FILE__)."/../includes/config.php");
if (!class_exists("Log"))
	require_once(dirname(__FILE__)."/log.php");

class Database {

    private $dbh = null;
    private static $instance = null;

    public static function getInstance() {
        try {
            if(self::$instance == null) {
                Log::logInfo("open connection to postgre database [".DBNAME."]");
                self::$instance = new Database();
            }
            Log::logSuccess("Database connection ok");
            return self::$instance->dbh;
        } catch (PDOException $e) {
            Log::logError("opendb error!: $e->getMessage()");
        }
    }
    
    private function __construct() {
    	$this->dbh = pg_connect("host=".HOST." dbname=".DBNAME." user=".USER." password=".PASSWORD);
    }
    
    // close connection on destruct
    public function __destruct() {
    	Log::logInfo("closing connection to [".DBNAME."]");
    	pg_close($this->dbh);
    }
}